package leetcode;

import java.util.ArrayList;

public class Q0071 {
    public String simplifyPath(String path) {
        String[] a = path.split("/");
        ArrayList<String> paths = new ArrayList<>();
        for (String s : a) {
            if (s.isEmpty() || s.equals(".")) continue;
            if (s.equals("..")) {
                if (!paths.isEmpty()) paths.remove(paths.size() - 1);
            } else {
                paths.add(s);
            }
        }
        if (paths.isEmpty()) return "/";
        StringBuilder sb = new StringBuilder();
        for (String s : paths) {
            sb.append('/').append(s);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Q0071 sl = new Q0071();
        String path = "/home//foo/";
        System.out.println(sl.simplifyPath(path));
    }
}
